package com.cgbl.cg.vm.home

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableField
import api.CourseServiceFactory
import api.UserServiceFactory
import com.alibaba.fastjson.JSON
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.common.api.CourseService
import com.robot.base.configs.AppConstants
import com.robot.base.model.*
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class HomeCourseViewModel(application: Application) : GLBaseViewModel(application) {
    /**
     * 热门课程推荐，后台作品列表配置，热门推荐，
     * 由排序小到大进行排序显示数量为4个作品。
     * 默认如果未获取到已上架的作品，或获取少于2个，则整个区域不显示
     * 点击单个进入课程详情页
     */
    val recommendCourseVisit = ObservableField(true)

    val uc = UiChangeEvent()
    var position = 0
    var data: List<CourseAllTypeBean>? = null

    class UiChangeEvent {
        val tabData: SingleLiveEvent<List<TypeListBean>> = SingleLiveEvent()

        val hotData: SingleLiveEvent<List<CourseBean>> = SingleLiveEvent()

        val systemData: SingleLiveEvent<List<CourseBean>> = SingleLiveEvent()

        val moreData: SingleLiveEvent<List<CourseBean>> = SingleLiveEvent()

        val refreshEvent: SingleLiveEvent<Int> = SingleLiveEvent()

        val ribbonDataEvent = SingleLiveEvent<List<BannerBean>>()
    }


    val toExcellentCourseCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.COURSE.A_EXCELLENT_COURSE,
            Bundle().apply {
                putInt(AppConstants.BundleKey.TYPE, 0)
            })
    })

    val toVipCourseCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.COURSE.A_EXCELLENT_COURSE,
            Bundle().apply {
                putInt(AppConstants.BundleKey.TYPE, 1)
            })
    })

    val toFreeCourseCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.COURSE.A_EXCELLENT_COURSE,
            Bundle().apply {
                putInt(AppConstants.BundleKey.TYPE, 2)
            })
    })

    val toLimitedTimeCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.COURSE.A_EXCELLENT_COURSE,
            Bundle().apply {
                putInt(AppConstants.BundleKey.TYPE, 3)
            })
    })

    val toCourseMenuCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(AppConstants.Router.COURSE.A_COURSE_MENU,
            Bundle().apply {
                putString(
                    AppConstants.BundleKey.CLASSIFICATION_DATA,
                    JSON.toJSONString(data)
                )
                putInt(
                    AppConstants.BundleKey.CLASSIFICATION_FIRST_POSITION,
                    position
                )

                putInt(
                    AppConstants.BundleKey.CLASSIFICATION_SECOND_POSITION, 0
                )
            })
    })

    fun refresh() {
        getRibbonList()
        fetchCourseList()
        page()
    }

    fun fetchCourseList() {
        CourseServiceFactory.homeData()
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<CourseHomeBean>>(this) {
                override fun onNext(t: BaseResponse<CourseHomeBean>) {
                    uc.refreshEvent.postValue(0)
                    uc.hotData.postValue(t.data.hotList)
                    uc.tabData.postValue(t.data.typeList)
                    uc.systemData.postValue(t.data.systemList)
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    uc.refreshEvent.postValue(0)
                }
            })
    }

    fun page() {
        CourseServiceFactory.page(
            mapOf(
                "pageNo" to 1,
                "pageSize" to 4,
                "sortType" to 1
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<CourseBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<CourseBean>>) {
                uc.refreshEvent.postValue(0)
                uc.moreData.postValue(t.data.list)
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.refreshEvent.postValue(0)
            }
        })
    }

    fun fetchCourseClassificationData() {
        CourseServiceFactory.allType().subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<List<CourseAllTypeBean>>>(this) {
            override fun onNext(t: BaseResponse<List<CourseAllTypeBean>>) {
                data = t.data as MutableList<CourseAllTypeBean>?
            }
        })
    }

    fun getRibbonList(){
        UserServiceFactory.getBannerList(
            1
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<List<BannerBean>>>(this){
            override fun onNext(t: BaseResponse<List<BannerBean>>) {
                uc.ribbonDataEvent.postValue(t.data)
            }
        })
    }
}